package cn.ainit.bj.flow;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ainit.bj.flow.entity.FlowNode;
import cn.ainit.sys.common.dao.BusinessBaseDao;
import cn.ainit.sys.ds.cloumns.sysCloumns;
import cn.ainit.sys.menu.SysFunctions;

@Transactional
@Repository
public class BjFlowDaoImpl extends BusinessBaseDao implements BjFlowDao {
	@Override
	public FlowNode getDsIdForNode(String nodeId) throws Exception {
		FlowNode flowNode=null;
		try {
			flowNode = (FlowNode) this.getSessionFactory().getCurrentSession().get(FlowNode.class, nodeId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flowNode;
	}
	 @Override
	public String execjudgenode(String manageId,String dsId,String uid) throws Exception {
	    String putValue="";
		Connection conn= null;
		CallableStatement cs = null;
		try {
			conn= this.getJdbcTemplate().getDataSource().getConnection();
			conn.setAutoCommit(false);
			 String sql = "{Call PK_BJ_XT.up_GetManageDjID(?,?,?,?)}";
			 cs = conn.prepareCall(sql);
			 cs.setString(1,manageId);
			 cs.setString(2,dsId);
			 cs.setString(3,uid);
			 cs.registerOutParameter(4,oracle.jdbc.OracleTypes.VARCHAR);
			 cs.execute();
			 putValue = cs.getString(4);
			 System.out.println("manageId:-----"+manageId+"---dsId:----"+dsId+"---uid:----"+uid+"------返回值："+cs.getString(4));
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally{
			if(cs != null){
				cs.close();
			}
			if (conn!=null) {
				conn.close();
			}
		}
		return putValue;
	}
	@Override
	public List execSql(String sql) throws Exception {
		List list = new ArrayList();
		try {
			list = this.getJdbcTemplate().queryForList(sql);
			if (list != null) {
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public SysFunctions getSysFun(String dsid) throws Exception {
		if(dsid==null||"".equals(dsid.trim())){
			System.out.println("#76 BjFlowDaoImpl 中sys_function中dsID没有配置");
			return null;
		}
		Query query=this.sessionFactory.getCurrentSession().createQuery("from SysFunctions s where s.dsId like ?");  
		query.setString(0,"%"+dsid+"%");
        List<SysFunctions> funcs=query.list();
        if(funcs.size()>1){
			for (int i = 0; i < funcs.size(); i++) {
				String dsId=funcs.get(i).getDsId();
				String[] dsIds=dsId.split(";");
				for (int j = 0; j < dsIds.length; j++) {
					if(dsid.equals(j)){
						return funcs.get(i);
					}
				}
			}
        }
		return funcs.get(0);
	}
	@Override
	public String execProcForP(String manageId) throws Exception {
		 String putValue="";
			Connection conn= null;
			CallableStatement cs = null;
			try {
				conn= this.getJdbcTemplate().getDataSource().getConnection();
				String sql = "{? = call PK_BJ_XT.uf_GetParentDjInfo(?)}";
				cs = conn.prepareCall(sql);
				cs.registerOutParameter(1,Types.VARCHAR);
				cs.setString(2,manageId);
				cs.execute();
				putValue = cs.getString(1);
			} catch (Exception e) {
				e.printStackTrace();
				throw e;
			}finally{
				if(cs != null){
					cs.close();
				}
				if (conn!=null) {
					conn.close();
				}
			}
			return putValue;
	}
	@Override
	public List<Map<String, Object>> getFlowManager(String manageId)
			throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			String sql ="SELECT * FROM BS_FLOWMANAGER WHERE MANAGE_ID ='"+manageId+"'";
			list = this.getJdbcTemplate().queryForList(sql);
			if (list !=null) {
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public List<Map<String, Object>> getNodeById(String nodeId)
			throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			String sql ="SELECT * FROM BS_FLOWNODE WHERE FLOW_ID = '"+nodeId+"'";
			list = this.getJdbcTemplate().queryForList(sql);
			if (list !=null) {
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getMapInfo(String sql) {
		return this.getJdbcTemplate().queryForList(sql);
	}

	@Override
	public String getAHrefPkValue(String dsid, String djid, String colnName)
			throws Exception {
		Connection conn = null;
		CallableStatement cs = null;
		String pkId="";
		try {
			 conn =  this.getJdbcTemplate().getDataSource().getConnection();
			 String sql = "{?=call PK_SYS_UI.UF_GETAHREF_PKVALUE(?,?,?)}";
			 cs = conn.prepareCall(sql);
			 cs.registerOutParameter(1, Types.VARCHAR); 
			 cs.setString(2,dsid);
			 cs.setString(3,colnName);
			 cs.setString(4,djid);
			 cs.execute();
			 pkId = cs.getString(1);  
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(cs != null){
				cs.close();
			}
			if(conn != null){
				conn.close();
			}
		}
		return pkId;
	}
	
	
}
